﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using FXC.Web.Service;
using FXC.SL.CS.Util;

namespace FXC.SL.UserControls.Window
{
    public partial class AddingUserWindow : RadWindow
    {
        FXSystemContext _context = App.Current.Resources["FXSystemContext"] as FXSystemContext;

        public AddingUserWindow()
        {
            InitializeComponent();
        }

        private void radButtonOK_Click(object sender, RoutedEventArgs e)
        {
            if (!CheckInput())
                return;

            Web.Entity.User u = new Web.Entity.User();
            u.UserName = tbName.Text;
            u.Password = tbPwd.Password;
            u.Email = tbEmail.Text;
            u.PhoneNumber = tbPhone.Text;
            u.CountryCode = ((RadComboBoxItem)rcbCountry.SelectedItem).Tag.ToString();
            u.UpdateDate = DateTime.Now;
            u.CreateDate = DateTime.Now;
            u.IsActive = true;
            u.IsDeleted = false;
            _context.Users.Add(u);

            _context.SubmitChanges((a) =>
            {
                if (a.HasError)
                {
                    a.MarkErrorAsHandled();
                    MessageBox.Show("添加失败! 错误信息:" + a.Error.Message);
                    _context.RejectChanges();
                }
                else
                {
                    MessageBox.Show("添加成功！");
                    this.DialogResult = true;
                    ClearInput();
                    this.Close();
                }
            }, null);
            
        }

        private void radButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void RadWindow_Loaded(object sender, RoutedEventArgs e)
        {
            LoadCountryDropDownList();
        }

        void LoadCountryDropDownList()
        {
            foreach (var c in _context.Countries)
            {
                RadComboBoxItem rci = new RadComboBoxItem();
                rci.Content = c.ChineseName;
                rci.Tag = c.CountryCode;
                rcbCountry.Items.Add(rci);
            }
        }

        bool CheckInput()
        {
            if (string.IsNullOrEmpty(tbName.Text.Trim()))
            {
                tbNameTip.Style = App.Current.Resources["AddingUserFormHeaderTipError"] as Style;
                return false;
            }
            else
                tbNameTip.Style = App.Current.Resources["AddingUserFormHeaderTip"] as Style;

            if (string.IsNullOrEmpty(tbPwd.Password.Trim()))
            {
                tbPwdTip.Style = App.Current.Resources["AddingUserFormHeaderTipError"] as Style;
                return false;
            }
            else
                tbPwdTip.Style = App.Current.Resources["AddingUserFormHeaderTip"] as Style;

            if (!CommonCheck.IsValidEmail(tbEmail.Text.Trim()))
            {
                tbEmailTip.Style = App.Current.Resources["AddingUserFormHeaderTipError"] as Style;
                return false;
            }
            else
                tbEmailTip.Style = App.Current.Resources["AddingUserFormHeaderTip"] as Style;

            if (!CommonCheck.IsValidPhoneNo(tbPhone.Text.Trim()))
            {
                tbPhoneTip.Style = App.Current.Resources["AddingUserFormHeaderTipError"] as Style;
                return false;
            }
            else
                tbPhoneTip.Style = App.Current.Resources["AddingUserFormHeaderTip"] as Style;

            if (rcbCountry.SelectedItem==null)
            {
                tbCountryTip.Style = App.Current.Resources["AddingUserFormHeaderTipError"] as Style;
                return false;
            }
            else
                tbCountryTip.Style = App.Current.Resources["AddingUserFormHeaderTip"] as Style;


            return true;
        }

        void ClearInput()
        {
            tbEmail.Text = string.Empty;
            tbName.Text = string.Empty;
            tbPhone.Text = string.Empty;
            tbPwd.Password=string.Empty;
            rcbCountry.SelectedIndex = -1;
        }
    }
}
